package com.xsl.culture.mybasevideoview.view.secondui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xsl.culture.R;
import com.xsl.culture.mybasevideoview.MainActivity;
import com.xsl.culture.mybasevideoview.base.LanBaseActivity;
import com.xsl.culture.mybasevideoview.model.ClassEvent;
import com.xsl.culture.mybasevideoview.utils.LanguageManager;
import com.xsl.culture.mybasevideoview.utils.UiUtils;
import com.xsl.culture.mybasevideoview.utils.XslUtils;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DesActivity extends LanBaseActivity {
    @BindView(R.id.des)
    TextView textView;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.raidogroup)
    RadioGroup raidogroup;
    @BindView(R.id.zheng)
    RadioButton zheng;
    @BindView(R.id.simple)
    RadioButton simple;
    @BindView(R.id.english)
    RadioButton english;
    @BindView(R.id.language_image)
    ImageView language_image;

    Locale lang = Locale.SIMPLIFIED_CHINESE;
    Locale langCur = Locale.SIMPLIFIED_CHINESE;
    int langCode = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        UiUtils.hideBottomUIMenu(this);
        setContentView(R.layout.activity_description_layout);
        XslUtils.hideStausbar(new WeakReference<>(this), true);
        ButterKnife.bind(this);

        lang = LanguageManager.getSaveLanguageLocale(DesActivity.this);
        langCur = lang;
        raidogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.simple:
                        lang = Locale.SIMPLIFIED_CHINESE;
                        textView.setText(R.string.values_app_des_zh);
                        next.setText(R.string.values_next_zh);
                        langCode =1 ;
                        language_image.setImageResource(R.mipmap.language_bottom_ch);
                        break;
                    case R.id.zheng:
                        lang = Locale.TAIWAN;
                        textView.setText(R.string.values_app_des_tw);
                        next.setText(R.string.values_next_tw);
                        langCode =2 ;
                        language_image.setImageResource(R.mipmap.language_bottom_tw);
                        break;
                    case R.id.english:
                        lang = Locale.ENGLISH;
                        textView.setText(R.string.values_app_des_en);
                        next.setText(R.string.values_next_en);
                        langCode =3 ;
                        language_image.setImageResource(R.mipmap.language_bottom_en);
                        break;
                }
            }
        });
        if(lang == Locale.SIMPLIFIED_CHINESE){
            simple.setChecked(true);
        }else if(lang == Locale.TAIWAN){
            zheng.setChecked(true);
        }else if(lang == Locale.ENGLISH){
            english.setChecked(true);
        }
    }

    @OnClick(R.id.next)
    void closeClick() {

            if(SplashActivity.flag == 2){
                //启动
                LanguageManager.setSaveLanguage(this,lang.getLanguage()+lang.getCountry());
                LanguageManager.attachBaseContext(this);
                MainActivity.curLangugue = langCode;
                EventBus.getDefault().post("finish");
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }else{
                //其他界面切换
                if (langCur != lang) {
                    LanguageManager.setSaveLanguage(this, lang.getLanguage() + lang.getCountry());
                    LanguageManager.attachBaseContext(this);
                    MainActivity.curLangugue = langCode;
                    ClassEvent event = new ClassEvent();
                    event.msg = "do it";
                    EventBus.getDefault().post(event);
                }
                finish();
            }

    }

}
